use std::borrow::Cow;
use actix_web::{http::header::ContentType, HttpResponse, ResponseError};
use serde::Serialize;
use crate::error::AppErr;

use super::resp::{HEAD_RESP_TYPE, HEAD_TYPE_ERR};

#[derive(Debug, Serialize)]
struct ErrMsg {

    err_code: i32,
    err_msg: Cow<'static, str>,
}

impl ResponseError for AppErr {

    fn error_response(&self) -> HttpResponse {
        let err = ErrMsg {
            err_code: -1,
            err_msg: Cow::Owned(self.to_string()),
        };
        let cbor_bytes = serde_cbor::to_vec(&err).unwrap();
        HttpResponse::Ok()
            .content_type(ContentType::octet_stream())
            .insert_header((HEAD_RESP_TYPE, HEAD_TYPE_ERR))
            .body(cbor_bytes)
    }
}


